<template>
  <div>
    <div>
      <el-image style="animation: header-effect 2s"
                class="background-image"
                lazy
                :src="backgroundPath ? backgroundPath : $constant.bgImg"
                fit="cover">
        <div slot="error" class="image-slot background-image-error"></div>
      </el-image>
      <!-- 输入框 -->
      <div class="message-in" style="text-align: center">
        <h2 class="message-title">树洞</h2>
        <div>
          <input class="message-input"
                 type="text"
                 style="outline: none;width: 70%"
                 placeholder="留下点什么啦~"
                 v-model="content"
                 @keyup.enter="submitMessage()"
                 @click="show = true"
                 maxlength="60"/>
          <button v-show="show"
                  style="margin-left: 12px;cursor: pointer;width: 20%"
                  @click="submitMessage"
                  class="message-input">发射
          </button>
        </div>
      </div>
      <!-- 弹幕 -->
      <div class="barrage-container">
        <vue-baberrage :barrageList="barrageList" :loop="true"></vue-baberrage>
      </div>
    </div>
  </div>
</template>

<script>
import mixins from "@/utils/mixins";
export default {
  mixins: [mixins],
  components: {},
  data() {
    return {
      show: false,
      content: "",
      barrageList: []
    };
  },
  created() {
    this.getBarrage();
    this.getBackGroundImg();
  },
  methods: {
    getBarrage() {
      this.$createHttp.getBarrageList().then((res) => {
        if (res.code === 200) {
          res.data.forEach(m => {
            let barrage = {
              id: m.id,
              avatar: m.avatar,
              msg: m.content,
              time: Math.floor(Math.random() * 5 + 10)
            }
            this.barrageList.push(barrage);
          });
        }
      })
        .catch((error) => {
          this.$message({
            message: error,
            type: "error"
          });
        });
    },
    submitMessage() {
      if (this.$common.isEmpty(localStorage.getItem("token"))) {
        this.$message({
          message: "请先登录",
          type: "warning"
        });
        return;
      }

      if (this.content.trim() === "") {
        this.$message({
          message: "你还没写呢~",
          type: "warning"
        });
        return;
      }

      let barrage = {
        content: this.content.trim()
      };

      this.$createHttp.sendBarrage(barrage).then((res) => {
        if (res.code === 200) {
          let userInfo = this.$store.state.userInfo;
          this.barrageList.push({
            id: res.data,
            avatar: userInfo.avatar,
            msg: this.content
          });
        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: "error"
        });
      });
      this.content = "";
      this.show = false;
    }
  }
}
</script>

<style scoped>

.message-in {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  color: var(--white);
  animation: hideToShow 2.5s;
  width: 360px;
  z-index: 10;
}

.message-title {
  user-select: none;
  text-align: center;
}

.message-input {
  border-radius: 1.2rem;
  border: var(--white) 1px solid;
  color: var(--white);
  background: var(--transparent);
  padding: 10px 10px;
}

.message-input::-webkit-input-placeholder {
  color: var(--white);
}

.barrage-container {
  position: absolute;
  top: 50px;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(100% - 50px);
  width: 100%;
  user-select: none;
  overflow: hidden;
}
</style>
